package com.jinghang.wayofrich;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.jinghang.wayofrich.common.MenuUtil;
import com.jinghang.wayofrich.common.ViewUtils;
import com.jinghang.wayofrich.fragment.CardFragment;
import com.jinghang.wayofrich.fragment.IndexFragment;
import com.jinghang.wayofrich.fragment.MineFragment;
import com.jinghang.wayofrich.fragment.OrderFragment;

/**
 * 首页
 */
public class MainActivity extends AppCompatActivity {
    Context context;
    private Fragment mFragment=new OrderFragment();//当前显示的Fragment
    public static BottomNavigationBar mBottomNavigationBar;
    //菜单切换需要的参数
    private IndexFragment mIndexFragment;
    private CardFragment mCardFragment;
    private OrderFragment mIndentFragment;
    private MineFragment mMineFragment;
    private Fragment[] mFragments;
    private int index;//点击的fragment的下标
    private int currentTabIndex=0;//当前的fragment的下标

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        setContentView(R.layout.activity_main);
        ViewUtils.setImmersionStateMode(this);
        //初始化其他页面
        initView();
        //初始化底部菜单
        initMenu();
    }

    /**
     * 设置底部菜单
     */
    private void initMenu(){
        mBottomNavigationBar = findViewById(R.id.bottom_navigation_bar);
        String[] mTitles = new String[]{"首页", "办卡", "订单", "我的"};
        mBottomNavigationBar
                .setActiveColor(R.color.colorRed)
                .setInActiveColor(R.color.colorGray)
                .setBarBackgroundColor(R.color.colorRed);

        mBottomNavigationBar.setBarBackgroundColor(R.color.colorWhite);
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.menu_home_yes, mTitles[0])
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.drawable.menu_home_no)))
                .addItem(new BottomNavigationItem(R.drawable.menu_card_yes, mTitles[1])
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.drawable.menu_card_no)))
                .addItem(new BottomNavigationItem(R.drawable.menu_indent_yes, mTitles[2])
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.drawable.menu_indent_no)))
                .addItem(new BottomNavigationItem(R.drawable.menu_mine_yes, mTitles[3])
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.drawable.menu_mine_no)))
                .setFirstSelectedPosition(0)
                .initialise();//所有的设置需在调用该方法前完成

        //设置菜单图标和文字大小
        MenuUtil.setBottomNavigationItem(mBottomNavigationBar,context,9,18);
        //监听菜单事件
        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {//这里也可以使用SimpleOnTabSelectedListener
            @Override
            public void onTabSelected(int position) {//未选中 -> 选中
                switch (position){
                    case 0:
                        switchFragment(mIndexFragment);
                        break;
                    case 1:
                        switchFragment(mCardFragment);
                        break;
                    case 2:
                        switchFragment(mIndentFragment);
                        break;
                    case 3:
                        switchFragment(mMineFragment);
                        break;
                }
            }

            @Override
            public void onTabUnselected(int position) {//选中 -> 未选中

            }
            @Override
            public void onTabReselected(int position) {//选中 -> 选中

            }
        });
        switchFragment(mIndexFragment);
    }

    /**
     * 初始化其他页面
     */
    private void initView() {
        mIndexFragment =new IndexFragment();
        mCardFragment =new CardFragment();
        mIndentFragment =new OrderFragment();
        mMineFragment =new MineFragment();
    }

    /**
     * 切换页面
     * @param fragment
     */
    public void switchFragment(Fragment fragment) {
        //判断当前显示的Fragment是不是切换的Fragment
        if(mFragment != fragment) {
            //判断切换的Fragment是否已经添加过
            if (!fragment.isAdded()) {
                //如果没有，则先把当前的Fragment隐藏，把切换的Fragment添加上
                getSupportFragmentManager().beginTransaction().hide(mFragment)
                        .add(R.id.mainContent,fragment).commit();
            } else {
                //如果已经添加过，则先把当前的Fragment隐藏，把切换的Fragment显示出来
                getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit();
            }
            mFragment = fragment;
        }
    }
}
